import axios from '@/api/axios'
import urls from './urls'
import baseUrl from '@/api/baseUrl'
import Storage from '@/utils/storage'
const local = new Storage()
// const baseUrl = '/lrk'
export default {
    // 订单中心，基础通用接口
    orderGetDataAuthskip (params) {
        return axios.get(urls.orderGetDataAuthskip, {
            params
        })
    },
    // 商品品牌
    brandList (code) {
        return axios.get(urls.brandList, {
            headers: {
                OperationCode: code || 'stock_list'
            }
        })
    },
    // 库存中心商品来源
    getSourceFromData (code) {
        return axios.get(urls.getSourceFromData, {
            headers: {
                OperationCode: code || 'stock_list'
            }
        })
    },
    // 进度条
    loadingProgressAuthskip (params) {
        return axios.get(urls.loadingProgressAuthskip, { params })
    },
    // 获取仓库列表
    getWarehouseList (code) {
        return axios.get(urls.getWarehouseList, {
            headers: {
                OperationCode: code || 'stock_list'
            }
        })
    },
    getOptions (api, params) {
        if (api.url.includes('yapi')) {
            return axios.get(`${api.url}`, {
                params,
                headers: { OperationCode: api.operationCode }
            })
        }
        if (local.get(`${api.url}`) && params && !params.refresh) {
            let result = local.get(`${api.url}`)
            if (params && params.kwd && params['key']) {
                result = result.filter(el => el[params['key']].includes(params.kwd))
            }
            if (params && params.currentPage) {
                result = result.slice((params.currentPage - 1) * 20, 20 * params.currentPage)
            }
            // console.log(result, 'result')
            return Promise.resolve({
                success: true,
                result
            })
        }
        // params['key'] = params.kwd
        try {
            return new Promise((resolve, reject) => {
                axios.get(`${baseUrl}${api.url}`, {
                    params,
                    headers: { OperationCode: api.operationCode }
                }).then(({ success, result }) => {
                    if (result == null) {
                        resolve({
                            result: [],
                            success
                        })
                        return
                    }
                    if (result.page) {
                        resolve({
                            page: result.page,
                            result: (result[0] && result[0].dataList) || result.records || [],
                            success
                        })
                        return
                    }
                    result = (result[0] && result[0].dataList) || result
                    local.set(`${api.url}`, result, 30 * 1000)
                    if (params && params.kwd && params['key']) {
                        result = result.filter(el => el[params['key']].includes(params.kwd))
                    }
                    if (params && params.currentPage) {
                        result = result.slice((params.currentPage - 1) * 20, 20 * params.currentPage)
                    }
                    resolve({
                        result,
                        success
                    })
                }).catch(err => reject({}))
            })
        } catch (error) {
            return Promise.reject(error)
        }
    },
    // 获取所有地区
    getRegionData (params, code) {
        return axios.get(urls.regionData, {
            params,
            headers: { OperationCode: code || 'sell_regional' }
        })
    },
    // 获取导入错误的文件路径
    errorFileAuthskip (data, code) {
        return axios.get(urls.errorFileAuthskip, {
            params: data,
            headers: {
                OperationCode: code || 'order_manage'
            }
        })
    },
    // 获取库存导入错误的文件路径
    stockImportErrorFile (data, code) {
        return axios.get(urls.stockImportErrorFile, {
            params: data,
            headers: {
                OperationCode: code || 'order_manage'
            }
        })
    },
    // 库存导入进度条
    stockImportProgressBar (data, code) {
        return axios.get(urls.stockImportProgressBar, {
            params: data,
            headers: {
                OperationCode: code || 'order_manage'
            }
        })
    }
}
